package com.wang.lotterysystem.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wang.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.wang.lotterysystem.common.exception.ControllerException;
import com.wang.lotterysystem.common.pojo.CommonResult;
import com.wang.lotterysystem.controller.param.CreateActivityParam;
import com.wang.lotterysystem.controller.param.PageListParam;
import com.wang.lotterysystem.controller.result.activity.CreateActivityResult;
import com.wang.lotterysystem.controller.result.activity.FindActivityListResult;
import com.wang.lotterysystem.controller.result.activity.GetActivityDetailResult;
import com.wang.lotterysystem.service.ActivityService;
import com.wang.lotterysystem.service.dto.activity.ActivityDTO;
import com.wang.lotterysystem.service.dto.activity.ActivityDetailDTO;
import com.wang.lotterysystem.service.dto.activity.CreateActivityDTO;
import com.wang.lotterysystem.service.dto.PageListDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Tag(name = "活动相关功能接口")
@RestController
@Slf4j
public class ActivityController {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private ActivityService activityService;

    @Operation(summary = "创建一个抽奖活动")
    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult> createActivity(@Validated @RequestBody CreateActivityParam param) throws JsonProcessingException {
        //打印日志
        log.info("create activity:{}",objectMapper.writeValueAsString(param));
        //调用Service层的createActivity()方法
        CreateActivityDTO createActivityDTO = activityService.createActivity(param);
        //将结果转换为CommonResult并返回
        return CommonResult.success(convertToCreateActivityResult(createActivityDTO));
    }

    //转换方法
    private CreateActivityResult convertToCreateActivityResult(CreateActivityDTO createActivityDTO) {
        if (createActivityDTO == null){
            throw new ControllerException(ControllerErrorCodeConstants.ACTIVITY_CREATE_ERROR);
        }
        CreateActivityResult result = new CreateActivityResult();
        result.setActivityId(createActivityDTO.getActivityId());
        return result;
    }

    /**
     * 获取活动列表
     * @param param 活动和奖品id
     * @return 返回活动列表
     * @throws JsonProcessingException json异常
     */
    @Operation(summary = "获取活动列表")
    @RequestMapping("/activity/find-list")
    public CommonResult<FindActivityListResult> findActivityList(@Validated PageListParam param) throws JsonProcessingException {
        //打印日志
        log.info("find activity list:{}",objectMapper.writeValueAsString(param));
        //调用Service层的findActivityList()方法
        PageListDTO<ActivityDTO> pageListDTO = activityService.findActivityList(param);
        //将结果转换为CommonResult并返回
        return CommonResult.success(convertToFindActivityListResult(pageListDTO));
    }

    private FindActivityListResult convertToFindActivityListResult(PageListDTO<ActivityDTO> pageListDTO){
        if (pageListDTO == null){
            throw new ControllerException(ControllerErrorCodeConstants.ACTIVITIES_FIND_ERROR);
        }
        FindActivityListResult findActivityListResult = new FindActivityListResult();
        List<FindActivityListResult.ActivityInfo> activityInfoList = new ArrayList<>();
        findActivityListResult.setTotal(pageListDTO.getTotal());
        for (ActivityDTO activityDTO :pageListDTO.getRecords()) {
            FindActivityListResult.ActivityInfo activityInfo = new FindActivityListResult.ActivityInfo();
            activityInfo.setActivityId(activityDTO.getActivityId());
            activityInfo.setActivityName(activityDTO.getActivityName());
            activityInfo.setDescription(activityDTO.getDescription());
            activityInfo.setValid(activityDTO.valid());
            activityInfoList.add(activityInfo);
        }
        findActivityListResult.setRecords(activityInfoList);
        return findActivityListResult;
    }

    /**
     * 获取活动详细信息
     * @param activityId 活动id
     * @return 返回活动详细信息
     */
    @Operation(summary = "获取活动详细信息")
    @RequestMapping("/activity-detail/find")
    public CommonResult<GetActivityDetailResult> getActivityDetail(@NotNull Long activityId) throws JsonProcessingException {
        //打印日志
        log.info("activity id :{}",activityId);
        //调用Service层的getActivityDetail()方法
        ActivityDetailDTO activityDetail = activityService.getActivityDetail(activityId);
        //将结果转换为CommonResult并返回
        GetActivityDetailResult result = convertGetActivityDetailResult(activityDetail);
        return CommonResult.success(result);
    }

    private GetActivityDetailResult convertGetActivityDetailResult(ActivityDetailDTO activityDetail) {
        if (activityDetail == null){
            log.warn("活动详细信息为空");
            throw new ControllerException(ControllerErrorCodeConstants.GET_ACTIVITY_DETAIL_ERROR);
        }
        GetActivityDetailResult result = new GetActivityDetailResult();
        result.setActivityId(activityDetail.getActivityId());
        result.setActivityName(activityDetail.getActivityName());
        result.setDescription(activityDetail.getDescription());
        result.setValid(activityDetail.valid());
        List<GetActivityDetailResult.Prize> prizeList = new ArrayList<>();
        for (ActivityDetailDTO.PrizeDTO prizeDTO :activityDetail.getPrizeDTOList()) {
            GetActivityDetailResult.Prize prize = new GetActivityDetailResult.Prize();
            prize.setPrizeId(prizeDTO.getPrizeId());
            prize.setName(prizeDTO.getName());
            prize.setImageUrl(prizeDTO.getImageUrl());
            prize.setPrice(prizeDTO.getPrice());
            prize.setDescription(prizeDTO.getDescription());
            prize.setPrizeTierName(prizeDTO.getTiers().getMessage());
            prize.setPrizeAmount(prizeDTO.getPrizeAmount());
            prize.setValid(prizeDTO.valid());
            prizeList.add(prize);
        }
        result.setPrizes(prizeList);
        List<GetActivityDetailResult.User> users = new ArrayList<>();
        for (ActivityDetailDTO.UserDTO userDTO :activityDetail.getUserDTOList()) {
            GetActivityDetailResult.User user = new GetActivityDetailResult.User();
            user.setUserId(userDTO.getUserId());
            user.setUserName(userDTO.getUserName());
            user.setValid(userDTO.valid());
            users.add(user);
        }
        result.setUsers(users);
        return result;
    }
}